<template>
  <div class="permission-container">
    <div>
      <el-button type="primary" size="mini" @click="addClick(1,0)">添加权限</el-button>
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        row-key="id"
      >
        <el-table-column
          prop="name"
          label="名称"
        />
        <el-table-column
          prop="code"
          label="标识"
        />
        <el-table-column
          prop="description"
          label="描述"
        />
        <el-table-column
          fixed="right"
          label="操作"
          width="180"
        >
          <template slot-scope="{row}">
            <el-button v-if="row.type === 1" type="text" size="small" @click="addClick(2,row.id)">添加</el-button>
            <el-button type="text" size="small" @click="toClickTmp(row.id)">编辑</el-button>
            <el-button type="text" size="small" @click="deleClick(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 对话框组件 -->
    <el-dialog :title="isEdit? '编辑权限点':'新增权限点'" :visible.sync="dialogFormVisible" :modal-append-to-body="false" width="40%" @close="close">
      <el-form ref="ruleFormsdfs" :model="Form" :rules="rules">
        <el-form-item label="权限名称" label-width="150px" prop="name">
          <el-input v-model="Form.name" />
        </el-form-item>
        <el-form-item label="权限标识" label-width="150px" prop="code">
          <el-input v-model="Form.code" />
        </el-form-item>
        <el-form-item label="权限描述" label-width="150px" prop="description">
          <el-input v-model="Form.description" />
        </el-form-item>
        <el-form-item label="开启" label-width="150px" prop="enVisible">
          <el-switch
            v-model="Form.enVisible"
            active-color="#13ce66"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item label-width="150px">
          <el-button type="primary" :loading="flag" @click="submitForm">{{ isEdit? "更新" : "确 定" }}</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// 导入封装的接口
import { getpermissionRequest, addPermissionRequest, deletePermissionRequest, getPermissionDetailRequest, updatePermissionRequest } from '@/api/permission.js'
// 导入封装的递归函数
import { getsome } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      // 表格数据
      tableData: [],
      // 对话框开启条件
      dialogFormVisible: false,
      // 对话框数据
      Form: {
        name: '',
        code: '',
        description: '',
        enVisible: 0,
        type: null,
        pid: null
      },
      // 校验
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
      },
      // 判断是编辑还是新增
      isEdit: false,
      flag: false

    }
  },
  created() {
    this.getpermissionList()
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2) {
        return 'warning-row'
      } else {
        return 'success-row'
      }
    },
    // 封装获取权限列表函数
    async getpermissionList() {
      const data = await getpermissionRequest()
      console.log(data)
      this.tableData = getsome(data.data, 0)
    },
    // 点击弹出对话框
    addClick(type, id) {
      this.isEdit = false
      this.dialogFormVisible = true
      this.Form.type = type
      this.Form.pid = id
    },
    // 对话框事件
    close() {
      this.$refs.ruleFormsdfs.resetFields()
      this.Form = this.$options.data().Form
      this.dialogFormVisible = false
      this.isEdit = false
    },
    // 点击确定添加/修改
    submitForm() {
      this.$refs.ruleFormsdfs.validate(async valid => {
        if (valid) {
          try {
            this.flag = true
            if (this.isEdit) {
              await updatePermissionRequest(this.Form)
            } else {
              await addPermissionRequest(this.Form)
            }
            this.flag = false
            // 关闭弹窗
            this.close()
            // 渲染页面
            this.getpermissionList()
          } catch (error) {
            this.flag = false
          }
        } else {
          console.log('验证失败')
        }
      })
    },
    // 点击删除
    deleClick(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deletePermissionRequest(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 刷新页面
        this.getpermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 点击编辑
    async toClickTmp(id) {
      this.isEdit = true
      this.dialogFormVisible = true
      const data = await getPermissionDetailRequest(id)
      this.Form = data.data
    }
  }
}
</script>

<style lang="scss" scoped>
.permission-container{
  padding: 20px;
}

</style>
<style>
  .el-table .warning-row {
    background: #fff;
  }

  .el-table .success-row {
    background: #ebf9f9;
  }
</style>
